package com.adwin.addcoin.ui.main

import android.app.Application
import android.util.Log
import androidx.lifecycle.AndroidViewModel
import androidx.lifecycle.MutableLiveData
import com.adwin.addcoin.repository.SchedulerTransformer
import com.adwin.addcoin.repository.api.TestApi
import com.adwin.addcoin.repository.bean.HomeTaskBean
import com.adwin.addcoin.repository.net.RetrofitClient
import com.adwin.addcoin.utils.PersonalMessageUtils
import okhttp3.MediaType
import okhttp3.MultipartBody
import okhttp3.RequestBody
import org.json.JSONObject

class MainViewModel(application: Application) :AndroidViewModel(application) {

    var taskLists = MutableLiveData<ArrayList<HomeTaskBean>>()
    var isLoad = MutableLiveData(false)
    var isRefresh = MutableLiveData(false)
    var pageSize = 15
    /**
     *
     */
    fun getAppDisposition(){
        RetrofitClient.getInstance().getService(TestApi::class.java).getAppDisposition().compose(
            SchedulerTransformer()
        ).subscribe({
            if (it.isSuccessful){
                if (it.body()?.code == 200){
                    it.body()?.data?.let {bean ->
                        PersonalMessageUtils.setAppDispositionBean(bean)
                    }
                }
            }
        },{

        })
    }

    /**
     * 获取当前用户邀请详情
     */
    fun getInviteDetails(){
        RetrofitClient.getInstance().getService(TestApi::class.java)
            .getInviteDetails().compose(
            SchedulerTransformer()
        ).subscribe({
            if (it.isSuccessful){
                if (it.body()?.code == 200){
                    it.body()?.data?.let { inviteDetailsBean ->
                        PersonalMessageUtils.setInviteDetailsBean(inviteDetailsBean)
                    }
                }
            }
        },{

        })
    }

    /**
     * 获取活动任务列表
     */
    fun getTaskList(pageNumber:Int){
        isRefresh.value = true
        val json = JSONObject()
        json.put("pageNum",pageNumber)
        json.put("pageSize",pageSize)
        json.put("status",0)
        val requestBody: RequestBody = MultipartBody.create(MediaType.get("application/json;charset=utf-8"), json.toString())
        RetrofitClient.getInstance().getService(TestApi::class.java)
            .getTaskList(requestBody).compose(
            SchedulerTransformer()
        ).subscribe({
            if (it.isSuccessful){
                it.body()?.let { data ->
                    if (data.code == 200){
                        if (pageNumber == 1){
                            taskLists.value?.clear()
                        }
                        val lists = ArrayList<HomeTaskBean>()
                        taskLists.value?.let {
                            lists.addAll(it)
                            isLoad.value = it.size >= pageNumber
                        }

                        if (data.data!=null){
                            lists.addAll(data.data.list)
                            taskLists.value = lists
                        }
                    }
                }
            }

                isRefresh.value = false
        },{
                isRefresh.value = false
            Log.d("taskError","error:${it.message}")
        })
    }

    /**
     * 查询当前用户金币详情
     */
    fun  queryUserCoinDetail(){
        RetrofitClient.getInstance().getService(TestApi::class.java).queryUserCoinDetail().compose(
            SchedulerTransformer()
        ).subscribe({
            if (it.isSuccessful){
                it.body()?.let {coinsBean ->
                    if (coinsBean.code == 200){
                        PersonalMessageUtils.setUserCoins(coinsBean.data)
                    }
                }
            }
        },{

        })
    }


}